<!--
 * @Autor: curry
 * @Description: 
 * @Date: 2022-11-28 15:15:14
-->
<template>
  <div class="com-container" @dblclick="resertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import axios from "axios"
import { mapState } from "vuex";
import { getProvinceMapInfo } from '@/utils/map_utils'
export default {
  name: "Map",

  data() {
    return {
      chartInstance: null,
      allData: null, //获取到的数据
      privinceData:{},//缓存省份数据
    };
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme(){
      this.chartInstance.dispose()
      this.initChart();
      this.screenAdapter()
      this.updateCharts()
    }
  },
  created () {
    this.$socket.registerCallBack('mapData', this.getData);
  },

  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'mapData',
      chartName: 'map',
      value: ''
    });
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    // this.$socket.unRegisterCallBack("mapData");
  },

  methods: {
    // 初始化
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, this.theme);
      const res = await axios.get(
        "http://localhost:8999/static/map/china.json"
      );
      // 注册
      this.$echarts.registerMap("china", res.data);
      //   配置
      const initOption = {
        title: {
          text: '▎ '+"商家分布图",
          left: 20,
          top: 20,
        },
        geo: {
          type: "map",
          map: "china",
          top: "5%",
          bottom: "5%",
          itemStyle:{
            areaColor:"#2e728f",//区域颜色
            borderColor:"#333"
          },
        },
        legend:{
          left:'5%',
          bottom:'5%',
          orient:'vertical',//方向
        }
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on('click',async (arg)=>{
        const provinceInfo =  getProvinceMapInfo(arg.name)
        // 获取响应省份的数据
        if (!this.privinceData[provinceInfo.key]) {
          let res = await axios.get(`http://localhost:8999${provinceInfo.path}`)
          this.privinceData[provinceInfo.key] = res.data
          this.$echarts.registerMap(provinceInfo.key, res.data);
        }
        
        
        const changeOption = {
          geo:{
            map: provinceInfo.key,
          }
        }
        this.chartInstance.setOption(changeOption)
      })
    },
    // 获取数据
    getData(res) {
      // const { data: res } = await this.$http.get("map");
      this.allData = res;
      this.updateCharts();
    },
    // 更新图表
    updateCharts() {
      // 图例数据
      const legendArr = this.allData.map((item) => {
        return item.name;
      });
      const seriesArr = this.allData.map((item) => {
        return {
          type: "effectScatter",
          rippleEffect:{
            scale:5,//涟漪效果明显度
            brushType:'stroke',//空心效果
          },
          name: item.name,
          data: item.children,
          coordinateSystem: "geo", //地图显示散点数
          
        };
      });
      const dataOption = {
        legend: {
          data: legendArr,
        },
        series: seriesArr,
      };
      this.chartInstance.setOption(dataOption);
    },
    // 分辨率变化
    screenAdapter() {
      // 计算当前字体大小
      const titleFontSize = this.$refs.map_ref.offsetWidth / 100 * 3.6
      const uptateOption = {
        title:{
          textStyle: {
            fontSize:titleFontSize
          }
        },
        legend:{
          itemWidth:titleFontSize / 2,
          itemHeight:titleFontSize / 2,
          itemGap:titleFontSize / 2,
          textStyle: {
            fontSize:titleFontSize / 2
          }
        }
      };
      this.chartInstance.setOption(uptateOption);
      this.chartInstance.resize();
    },
    resertMap(){
      this.initChart()
    }
  },
};
</script>

<style lang="scss" scoped></style>
